package com.ljy.my_study.leetcode.数据流中的第K大元素;

import java.util.PriorityQueue;

public class KthLargest {

    private PriorityQueue<Integer> queue;
    private int len;

    public KthLargest(int k, int[] nums) {
        queue=new PriorityQueue<>((a,b)->a.compareTo(b));
        this.len=k;
        for(int i:nums){
            add(i);
        }
    }

    public int add(int val) {
        queue.offer(val);
        if(queue.size()>len){
            queue.poll();
        }
        return queue.peek();
    }
}
